﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using Abp.Domain.Repositories;
using FwProject.Authorization.Users;
using FwProject.Car;
using FwProject.Car.Dto;
using FwProject.Controllers;
using FwProject.Core.Car;
using FwProject.Users;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Controllers
{
    [AbpMvcAuthorize]
    public class CarController : FwProjectControllerBase
    {
        private readonly ICarInfoAppService _carInfoAppService;
        private readonly IUserAppService _userAppService;
        private readonly IHostingEnvironment _env;

        public CarController(ICarInfoAppService carInfoAppService, IUserAppService userAppService, IHostingEnvironment env)
        {
            _userAppService = userAppService;
            _carInfoAppService = carInfoAppService;
            _env = env;
        }
        /// <summary>
        /// 获取汽车列表
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="pageSize"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<ActionResult> Index(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, string factoryid = "", string keyword = "")
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            if (size == 0)
                size = 10;

            if (Request.IsAjaxRequest())
            {
                var car = await _carInfoAppService.GetAllPagedListAsync(pageIdx, size, beginTime, endTime, factoryid, keyword);
                return PartialView("IndexPartial", car);
            }
            return View();
        }
        /// <summary>
        /// 创建汽车
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Create()
        {
            var model = await _userAppService.GetAllByTypeAsync(EmployeeType.司机类型, null, "");
            ViewBag.Driver = model.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.Name });
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> CreateCar(CarInfoInputDto input)
        {
            var model = await _carInfoAppService.Create(input);
            return Json(model);
        }
        /// <summary>
        /// 编辑汽车信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> Edit(Guid id)
        {
            var carmodel = await _carInfoAppService.GetByCarIdAsync(id);
            var model = await _userAppService.GetAllByTypeAsync(EmployeeType.司机类型, null, "");
            ViewBag.Driver = model.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.Name });
            return View(carmodel);
        }
        [HttpPost]
        public async Task<JsonResult> EditCar(Guid id, CarInfoInputDto input)
        {
            await _carInfoAppService.Update(id, input);
            return Json(id);
        }
        /// <summary>
        /// 删除汽车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JsonResult> DeleteCar(Guid id)
        {
            await _carInfoAppService.Delete(id);
            return Json(id);
        }
        public async Task<IActionResult> ExportExcel()
        {
            var list = await _carInfoAppService.GetCarListAsync();
            return DoExcel<CarInfoDto>(list, _env);
        }
    }
}
